$(function () {
    var key = []
    $("#search").on("change",searchChange)
    searchChange()
    function searchChange() {
        if (localStorage["goods"]||localStorage["goods"]==[]) {//判断本地是否存在商品数据
            key = json(localStorage["goods"])//有则直接引用
        } else {//无则自建
            key[0] = {index: 0, img: "images/list/shangping1.jpg", p: "聚财兽·钱钱", small: "大“桔”大利 财源滚滚好彩头", money: 398}
            key[1] = {index: 1, img: "images/list/shangping2.jpg", p: "雪域燕麦芝士·轻享", small: "甄选OATLY燕麦露 轻享香甜浓滑", money: 298}
            key[2] = {index: 2, img: "images/list/shangping3.jpg", p: "雪域牛乳芝士", small: "经典之作 热销多年", money: 218}
            key[3] = {index: 3, img: "images/list/shangping4.jpg", p: "仙女塔", small: "情人节限定 予你甜蜜宠爱", money: 336}
            key[4] = {index: 4, img: "images/list/shangping5.bmp", p: "数字蛋糕", small: "人见人爱火爆", money: 218}
            key[5] = {index: 5, img: "images/list/shangping6.bmp", p: "巴斯克流心", small: "流心诱惑！风靡Ins的芝士蛋糕", money: 218}
            key[6] = {index: 6, img: "images/list/shangping7.bmp", p: "环游世界", small: "五星好评 9种口味一次过瘾", money: 258}
        }
        key = json(key); //转换为数组
        var newArr = [];// 动态的搜索的匹配的数据
        for (var i = 0; i < key.length; i++) {
//如果 有对应的关键字
            if (key[i].p.indexOf($("#search").val()) != -1) {
                newArr.push(key[i]); //压入新的数组
            }
        }
        var newObj = {
            list: newArr,  //已经筛选过的数据
        }
        var html = template("tem1", newObj);
        $("#list").html(html)
        localStorage["goods"] = JSON.stringify(key)//将商品存入
    }
})